import urllib.request
import urllib.parse
import ssl
import json

ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'

# 设置请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    "Cookie": "APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=869BC3E52B15685C4C074ECF0A12C297:FG=1; PSTM=1683722693; BIDUPSID=6FCA63A9E982AA241E73592173305871; BDUSS_BFESS=0VSbllDcUlWWDhjRE5La2M5Qk0zdkNrZ0M0dlEyeG92TX5hYkdBOEVoRWNYNFZrSUFBQUFBJCQAAAAAAAAAAAEAAADv4wlzc3VuMDMxN3RhbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABzSXWQc0l1kc; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1685364145,1685687468,1686355329,1686541114; BDSFRCVID=Hp4OJeC62RXlONrf7ImJJdafxPwbHgnTH6f3trVwxSgxYwqgMGUlEG0PUx8g0K4bZaqWogKKL2OTHm_F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=JnkqoKKXJCL3fP36qR7E-t4JhUTMb-vtHD7yWCvdblRcOR5Jj65WLxALLqjhtMoDtCTJBJA22lQqffJk3MA--t4w-JQqQM6nWjrrBpT9MpvIsq0x0b3te-bQyNOuq43x0DOMahv1tq7xObC9QlPK5JkgMx6MqpQJQeQ-5KQN3KJmfbL9bT3tjjTXeH8Ht6FjfR3ELTrH-P3OKROvhjR1K50gyxomtjjNyIJ92toDJhTVOlcg-tjW0b-VyMJXLUkq5J7JQfQ5MU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J8BtD-aMfK; delPer=0; PSINO=5; ZFY=R3IlA7y0akcZjF3EpoKGfHUVwvcltqn0DRlVKyEGgXA:C"
}

data = {
    "from": "en",
    "to": "zh",
    "query": "love",
    "transtype": "realtime",
    "simple_means_flag": "3",
    "sign": "198772.518981",
    "token": "62bdcc92c464e67ba263035e2c9c9969",
    "domain": "common",
    "ts": "168658692105"
}
# # post 请求的参数 必须进行编码
data = urllib.parse.urlencode(data).encode("utf-8")
# 请求对象的定制，  url地址，  参数，   请求头
request = urllib.request.Request(url=url, data=data, headers=headers)

respones = urllib.request.urlopen(request)

content = respones.read().decode('utf-8')
print(json.loads(content))
